package actions;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.StudentModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.AdminAppForm;
import forms.DegreeForm;
import forms.StudentForm;

public class AdminShowAppAction extends Action {
	
	private static final boolean DEBUG = true;
	//private static final int UNKNOWN = 0;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
		
		String fname = "";
		String mname = "";
		String lname = "";
		String citizen = "";
		String residence = "";
		String address = "";
		String city = "";
		String state="";
		String zip_code = "";
		String country_code= "";
		String area_code = "";
		String telephone = "";
		String residency = "";
		String specialization = "";
		String admission = "";
		
		Vector<DegreeForm> degrees = new Vector<DegreeForm>();
		DegreeForm degree;
		InputStream transcript;
		
		String string_val = "";
		int string_id = 0;
		
		// get app_id to look up student
		AdminAppForm aform = (AdminAppForm)form;
		// Look up student app id
		int stu_id = Integer.valueOf(aform.getApp_id());
		
		// Look up student application record
		RowSet rset = StudentModel.getStudent(stu_id);
		
		try {
			while (rset.next()) {
				// student names
				fname = rset.getString("first_name");
				lname = rset.getString("last_name");
				mname = rset.getString("middle_name");			
				// citizenship
				citizen = StudentModel.getString(StudentModel.StringToID.COUNTRY, 
								rset.getInt("citizenship"));
				// residence
				residence = StudentModel.getString(StudentModel.StringToID.COUNTRY, 
						rset.getInt("residence"));
				// address
				address = rset.getString("street_addr");
				city = rset.getString("city");
				state = rset.getString("state");
				zip_code = String.valueOf(rset.getInt("zip_code"));
				country_code = String.valueOf(rset.getInt("country_code"));				
				area_code = String.valueOf(rset.getInt("area_code"));
				telephone = String.valueOf(rset.getInt("telephone"));
				// residency
				residency = String.valueOf(rset.getBoolean("residency"));	
				// specialization
				specialization = StudentModel.getString(StudentModel.StringToID.SPECIAL, 
						rset.getInt("specialization"));
				admission = rset.getString("admission_status");
				
				if(DEBUG) {
					System.err.println("StudentAppAction: student found");
				}
			}
		} catch (SQLException e) {
			System.err.println("SQLException: " + e.getMessage());
		}
		
		rset = StudentModel.getDegrees(stu_id);
		
		try {
			while (rset.next()) {
				degree = new DegreeForm();
				
				string_val = StudentModel.getString(StudentModel.StringToID.UNIVERSITY, 
						rset.getInt("university"));
				degree.setUniversity(string_val);
				
				string_id = StudentModel.getUnivLocID(StudentModel.StringToID.UNIVERSITY, 
						string_val);
				string_val = StudentModel.getString(StudentModel.StringToID.LOCATION, 
						string_id);
				degree.setLocation(string_val);
				
				string_val = StudentModel.getString(StudentModel.StringToID.UNIVERSITY, 
						rset.getInt("discipline"));
				degree.setMajor(string_val);
				degree.setMonth(String.valueOf(rset.getInt("grad_month")));
				degree.setYear(String.valueOf(rset.getInt("grad_year")));
				degree.setGpa(String.valueOf(rset.getFloat("gpa")));
				degree.setTitle(rset.getString("title"));
				
				transcript = rset.getBinaryStream("transcript");
				degree.setFilename("transcript.pdf");
				try {
					degree.setFilesize(String.valueOf(transcript.available()));
				} catch (IOException e) {
					System.err.println("IOException: " + e.getMessage());
				}
				
				if(DEBUG) {
					System.err.println("StudentAppAction: degree found");
				}
				
				degrees.add(degree);
			}
		} catch (SQLException e) {
			System.err.println("SQLException: " + e.getMessage());
		}
		
		StudentForm app = new StudentForm();
		app.setFname(fname);
		app.setMname(mname);
		app.setLname(lname);
		app.setCitizen(citizen);
		app.setResidence(residence);
		app.setAddress(address);
		app.setCity(city);
		app.setState(state);
		app.setZip_code(zip_code);
		app.setArea_code(area_code);
		app.setTelephone(telephone);
		app.setResidency(residency);
		app.setSpecialization(specialization);
		
		// Store the student form into the request
		request.setAttribute("app_id", stu_id);
		request.setAttribute("app", app);
		request.setAttribute("degrees", degrees);
		
		if (DEBUG) {
			System.err.println("AdminShowAppAction: application received");
		}
		
		return mapping.findForward("success");
	}
}
